<?php
/**
 * 《电话》实用类
 *
 * @author 孙晓晔
 * @version $Id: TelUtil.class.php 1033 2010-03-25 11:26:55Z sunxy $
 */
require_once(dirname(__FILE__) . '/../AdminUtil.class.php');

class TelUtil extends AdminUtil {
	function __construct() {
		$request['update'] = '$Date: 2009/03/03 07:24:42 $';
		$request['revision'] = '$Revision: 1.5 $';
			
		parent::__construct($request);
	}

	function minute($time) {
		$minute = floor($time / 60);
		$second = $time % 60;

		if ($second > 0) {
			return $minute + 1;
		}

		return $minute;
	}
	
	function _local($s, $cdr = 'ns_sip') {
		require_once(APP_ROOT . "/../../z/Number.class.php");
		$number = new Number();

		if (preg_match('/^1(3|4|5|7|8)\d{9}$/i', $s)) {
			$ret = $number->mobile(substr($s, 0, 11));
			$ret['rate'] = 0.3;
			$ret['s'] = sprintf("%s [%s]", $ret['name'], $s);

			return $ret;
		}
		
		$zone = '0755';
		if ($cdr == 'qx_sip') {
			$zone = '0769';
		}

		if (preg_match('/^\d{7,8}$/i', $s)) {
			$ret = Number::fixed($zone . $s, $zone);
			$ret['rate'] = 0.22;
			$ret['s'] = sprintf("%s [%s]", $ret['name'], $s);

			return $ret;
		}

		$ret['s'] = $s;

		return $ret;
	}
	
	function ajax_number($controller, $request) {
		
		$number = $request['number'];
		$cdr = $request['cdr'];
		if (empty($cdr) || $cdr == '-') {
			$cdr = 'ns_sip';
		}
		
		$ret = $this->_number($number, $cdr);
		
		echo json_encode($ret);
	}
	
	function _number($s, $cdr = 'ns_sip') {
		if (preg_match("/^(2|3|6|8)\d{3}$/i", $s)) {
			return $this->_exten($s);
		}
		
		if (preg_match('/^\d{1,4}$/i', $s)) {
			$ret = array();
			$ret['s'] = $s;
			
			return $ret;
		}
		
		if (preg_match('/^6\d{2,5}$/i', $s)) {
			$ret = $this->_cm_dh($s);
			$ret['s'] = sprintf("%s [%s]", $ret['name'], $s);
			return $ret;
		}
		
		if (preg_match("/^(1)\d{5}$/", $s)) {
			
			return $this->_wintec($s);
		}
		
		if (preg_match('/^171/i', $s)) {
			$ret = $this->_vbuzzer(substr($s, 2), $cdr);
			$ret['s'] = sprintf("%s [%s]", $ret['name'], $s);

			return $ret;
		}
		
		if (preg_match('/^(1906)\d{2,5}$/i', $s)) {
			$ret = $this->_cm_dh(substr($s, 3));
			$ret['s'] = sprintf("%s [%s]", $ret['name'], $s);			
			return $ret;
		}
		
		if (preg_match('/^(194|195)/i', $s)) {
			$ret = $this->_out_9(substr($s, 3), 'ns_sip');
			$ret['s'] = sprintf("%s [%s]", $ret['name'], $s);
				
			return $ret;
		}
		
		if (preg_match('/^(196|197|198)/i', $s)) {
			$ret = $this->_out_9(substr($s, 3), 'qx_sip');
			$ret['s'] = sprintf("%s [%s]", $ret['name'], $s);
		
			return $ret;
		}
		
		if (preg_match('/^(17|19)/i', $s)) {
			$ret = $this->_country(substr($s, 5), $cdr);
			$ret['s'] = sprintf("%s [%s]", $ret['name'], $s);

			return $ret;
		}
		
		if (preg_match('/^0/i', $s)) {
			if (preg_match('/^00/i', $s)) {
				$ret = $this->_country(substr($s, 2), $cdr);
				$ret['s'] = sprintf("%s [%s]", $ret['name'], $s);
			
				return $ret;
			}
			
			$ret = $this->_china($s, $cdr);
			$ret['s'] = sprintf("%s [%s]", $ret['name'], $s);

			return $ret;
		}

		if (preg_match('/^9/i', $s)) {
			if (preg_match('/^96\d{2,5}/i', $s)) {
				$ret = $this->_cm_dh(substr($s, 1));
				$ret['s'] = sprintf("%s [%s]", $ret['name'], $s);
				return $ret;
			}
			
			if (preg_match('/^9(17909|11808|96688)00/i', $s)) {
				$ret = $this->_country(substr($s, 8), $cdr);
				$ret['s'] = sprintf("%s [%s]", $ret['name'], $s);
					
				return $ret;
			}
			
			$ret = $this->_out_9(substr($s, 1), $cdr);
			$ret['s'] = sprintf("%s [%s]", $ret['name'], $s);
			
			return $ret;
		}
		
		return $this->_local($s, $cdr);
	}

	/**
	 *
	 *
	 * @param string
	 * @return array
	 */
	function _out_9($s, $cdr = 'ns_sip') {
		if (preg_match('/^(17909|11808|96688)/i', $s)) {
			$ret = $this->_china(substr($s, 5), $cdr);
			$ret['s'] = sprintf("%s [%s]", $ret['name'], $s);
			$ret['rate'] = 0.3;

			return $ret;
		}
		
		if (preg_match('/^600/i', $s)) {
			$ret = $this->_cm_dh(substr($s, 3), $cdr);
			$ret['s'] = sprintf("%s [%s]", $ret['name'], $s);
		
			return $ret;
		}

		$ret = $this->_china($s, $cdr);
		$ret['s'] = sprintf("%s [%s]", $ret['name'], $s);

		return $ret;
	}
	
	function _cm_dh($dh) {
		$key = "_cm_" . $dh;
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}
	
		$db = newdb(Constant::tbl_emp);
		$fmt = "SELECT id, name FROM %s WHERE short = '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, Constant::tbl_emp, $dh);
		// debug($sql);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);
		
		$row['t'] = 'emp';
		$row['rate'] = 0.0;
	
		if (is_object($mc) && count($row) > 0) {
			$mc->set($key, $row, 0, _strtotime("3 day"));
		}
	
		return $row;
	}
	
	function _wintec($s) {
		$a = array(
			'11'=>'南山',
			'12'=>'沙井',
			'13'=>'清溪',
			'14'=>'永泰',
		);
		
		$b = substr($s, 0, 2);
		$c = substr($s, 2);
		
		$ret = array();
		
		if (preg_match("/^(15)/", $s)) {
			$ret['s'] = sprintf("%s [%s]", $a[$b], $s);
		} else {
			$d = $this->_exten($c);
			
			$ret['s'] = sprintf("%s [%s]", $d['name'], $s);
		}
		
		return $ret;
	}

	function _exten($exten) {
		require_once(dirname(__FILE__) . '/../emp/Exten.class.php');
		$aa = new Exten();
		
		if ($row = $aa->_view_exten($exten)) {
			$row['t'] = 'exten';
			$row['id'] = $row['emp'];
			if (!empty($row['name'])) {
				$name = $row['name'];
			} else {
				$name = $row['emp_t'];
			}
			
			$row['name'] = $name;
			$row['s'] = sprintf("%s [%s]", $name, $row['exten']);
			
			$row['rate'] = 0.0;
			
			return $row;
		}

		$ret = array();
		
		$ret['name'] = $exten;
		$ret['s'] = $exten;
			
		return $ret;		
	}
	
	function _vbuzzer($s, $cdr = 'ns_sip') {
		require_once(APP_ROOT . "/../../z/Number.class.php");
		$number = new Number();

		return $number->vbuzzer($s, $cdr);
	}

	function _country($s, $cdr = 'ns_sip') {
		require_once(APP_ROOT . "/../../z/Number.class.php");
		$number = new Number();

		return $number->country($s, $cdr);
	}

	function _china($s, $cdr = 'ns_sip') {
		require_once(APP_ROOT . "/../../z/Number.class.php");
		$number = new Number();

		return $number->china($s, $cdr);
	}

	function t($controller, $request) {
		$s = $this->aaaa('xz');
		echo $s;

		logdate($controller, $request);
		return true;
	}
	
	function aaaa($dept) {
		if (empty($dept) || $dept == '-') {
			return '';
		}

		$ret = " in ('0000";

		$db = newdb(Constant::tbl_emp);
		$fmt = "SELECT * FROM %s WHERE dept = '%s'";
		$sql = sprintf($fmt, Constant::tbl_emp, $dept);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$exten = $row['exten'];
			if (empty($exten)) {
				continue;
			}

			$ret .= "', '" . $exten;
		}
		$ret .= "')";

		return $ret;
	}
}